package com.martin.lib_base.bean.common

import androidx.compose.runtime.mutableStateOf
import dev.utils.common.Reflect2Utils
import kotlinx.coroutines.flow.MutableStateFlow

/**
 * @author：孟凡华
 * @date：2023/4/25
 * @desc：
 */
class KeyValueBean(key: String, value: Any) {

    // 键
    var key = mutableStateOf(key)

    // 值
    var value = mutableStateOf(value)


    // 是否选中
    val checked = mutableStateOf(false)

    // 值字符串
    val valueStr
        get() = value.value.toString()

    companion object {

        /**
         * 处理单选事件
         * 这里作为工具使用反射方式
         */
        @JvmStatic
        fun handleSingleChecked(list: List<Any>, position: Int, fieldName: String = "checked") {
            list.forEachIndexed { index, bean ->
                val checked =
                    Reflect2Utils.getPropertyByParent<MutableStateFlow<Boolean>>(bean, "checked")
                if (checked != null && checked.value != (position == index)) {
                    checked.tryEmit(position == index)
                }
            }
        }
    }

}